package com.kfm.mvn.servlet;


import com.kfm.mvn.util.Constant;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

@WebServlet("/out")
public class UserOutServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        if (id != null){
            ServletContext servletContext = req.getServletContext();
            Map<String, HttpSession> map = (Map<String, HttpSession>) servletContext.getAttribute(Constant.ONLINE_USERS_KEY);
            HttpSession remove = map.remove(id);
            if (remove != null){
                // session 过期
                remove.invalidate();
            }
            resp.sendRedirect(req.getContextPath() + "/online");
        }
    }
}
